<template>
	<yd-layout id='vipOpen' class=''>
		
		<div class="header_back"></div>
		<div class="header">
			<div v-if="dataObj.member_status==1" class="header_vip c_size_30_42" >已开通</div>
			<div v-else-if="dataObj.member_status==2" class="header_vip c_size_30_42" >已过期</div>
			<div v-else class="header_vip c_size_30_42" >暂未开通</div>
			<img class="header_img" :src="dataObj.avatar" alt="" />
			<div class="header_right">
				<div class="header_right_title ">
					<div class="title c_text_ellipsis_1">{{dataObj.nick_name}}</div>
					<img v-if='dataObj.member_status==1' class="vip_img" src="../../assets/vip_2.png" alt="" />
					<img v-if='dataObj.member_status==2' class="vip_img" src="../../assets/vip_1.png" alt="" />
				</div>
				<div class="c_size_30_42 header_right_label">{{dataObj.member_status!=0?dataObj.newTime+' 到期':'视频随意看，问题任意提'}}</div>
			</div>
		</div>
		<div class="down_app" v-if="false">
			<div @click="goDownApp" class="down_app_label">点击下载中鼎恒生商学院APP</div>
		</div>
		<div class="content">
			<div class="content_title c_size_34_48">会员福利</div>
			<img class="content_img" :src="urlImg" alt="" />
		</div>
		
		<div slot='bottom' class="bottom" >
			<div class="bottom_money" @click="goExchange">兑换会员</div>
			<div class="btn" @click="goWallet">{{dataObj.member_status!=0?'立即续费':'立即开通'}}（{{price}}元/年)</div>
		</div>
	</yd-layout>
</template>

<script>
	var pingpp = require('pingpp-js');
	export default{
		components:{
			
		},
		data(){
			return {
				dataObj:{},
				urlImg:'',
				price:0,
			}
		},
		created() {
		},
		watch: {
			
		},
		computed: {
			
		},
		mounted(){
			document.title='开通会员'
			this.$root.ajax({
				name:'member/index',
				type:'get',
				params:{
					
				}
			}).then((d)=>{
				this.urlImg = d.data.url
				d.data.me.newTime = this.$root.getDate(new Date(d.data.me.member_exp*1000))
				d.data.me.money=this.setMoneyZero(d.data.me,'money')
				d.data.me.tradeable_money=this.setMoneyZero(d.data.me,'tradeable_money')
				this.dataObj=Object.assign({},this.dataObj,d.data.me)
				this.price = d.data.price
			})
		},
		methods:{
			goDownApp(){
				window.location.href = this.$root.config.downAppUrl
			},
			//去兑换页面
			goExchange(){
				this.$router.push({
					name:'vipExchange'
				})
			},
			//处理金钱的0
			setMoneyZero(obj,str){
				if(obj[str]||obj[str]===0){
					if((obj[str]+'').indexOf('.')>=0){
						return obj[str]=obj[str].toFixed(1)
					}else{
						return obj[str] = obj[str] + '.0'
					}
				}
			},
			async goWallet(){
				if(this.$root.config.openId){
					
				}else{
					await this.$root.getOpenId()
				}
				this.$root.ajax({
					name:'member/purchase',
					params:{
						type:2,
						channel:4,
						client:3,
						//success_url:window.location.href
						//success_url:`${this.$root.config.shareUrl}#/success`
						success_url:this.$root.config.openId
					}
				}).then((d)=>{
					d.data.channel='wx_pub'
					pingpp.createPayment(d.data, (result, err)=>{
						// object 需是 Charge/Order/Recharge 的 JSON 字符串
						// 可按需使用 alert 方法弹出 log
						//console.log(JSON.stringify(object));
					    if (result == "success") {
					    	window.location.reload(); 
					    	// 只有微信JSAPI (wx_pub)、微信小程序（wx_lite）、QQ 公众号 (qpay_pub)、支付宝小程序（alipay_lite）支付成功的结果会在这里返回，其他的支付结果都会跳转到 extra 中对应的 URL
					    } else if (result == "fail") {
					    	this.$dialog.toast({
				                mes: err.msg,
				                timeout: 1500
				            });
					        // Ping++ 对象 object 不正确或者微信JSAPI/微信小程序/QQ公众号支付失败时会在此处返回
					    } else if (result == "cancel") {
					    	this.$dialog.toast({
			                    mes: '已取消支付！',
			                    timeout: 1500
			                });
					        // 微信JSAPI、微信小程序、QQ 公众号、支付宝小程序支付取消支付
					    }
					});
				})
			},
		}
	}
</script>

<style lang="scss">
	@import './vipOpen.scss';
</style>